package com.doform.android.xianggucheng.activity;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.doform.android.xianggucheng.BaseTimePickerActivity;
import com.doform.android.xianggucheng.R;
import com.doform.android.xianggucheng.bean.SalesBean;
import com.doform.android.xianggucheng.bean.request_bean.ReqSale;
import com.doform.android.xianggucheng.retrofit.subscriber.ErrorHandleSubscriber;
import com.doform.android.xianggucheng.retrofit.HttpManager;
import com.doform.android.xianggucheng.widget.DividerItemDecoration;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.annotations.NonNull;

/**
 * Created by Administrator on 2017/5/10.
 */

public class SalesActivity extends BaseTimePickerActivity {

    @BindView(R.id.ll)
    LinearLayout mLL;

    @BindView(R.id.tv_query_start)
    TextView mQueryStart;

    @BindView(R.id.tv_query_stop)
    TextView mQueryStop;

    @BindView(R.id.btn_query)
    Button mQuery;

    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    private CommonAdapter<SalesBean.ListBean> mCommonAdapter;
    private String sdate;
    private String edate;

    @Override
    protected void showView() {
        titleBar.setTextTitle("产品销售");
        titleBar.setRightText("查询");
    }

    @Override
    protected int layoutResId() {
        return R.layout.activity_sales;
    }

    @Override
    protected void init() {
        super.init();
    }

    @Override
    protected void initView() {

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        long curTime = System.currentTimeMillis();
        long startTime = curTime - 7 * 24 * 60 * 60 * 1000;

        sdate = formatter.format(new Date(startTime));
        edate = formatter.format(new Date(curTime));

        mQueryStart.setText(sdate);
        mQueryStop.setText(edate);

        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
    }


    @Override
    protected void updateData() {
        super.updateData();
        getDataForService(sdate, edate);
    }

    private void getDataForService(String sdate, String edate) {
        HttpManager
                .instance()
                .getSale(new ReqSale(sdate, edate))
                .subscribe(new ErrorHandleSubscriber<SalesBean>() {
                               @Override
                               public void onNext(@NonNull SalesBean reportBean) {
                                   parseData(reportBean.getList());
                               }
                           }
                );
    }

    private void parseData(List<SalesBean.ListBean> data) {
        mDatas.clear();
        mDatas.addAll(data);
        if (mCommonAdapter != null) {
            mCommonAdapter.notifyDataSetChanged();
        }
    }


    @Override
    protected void initListener() {
        titleBar.setRightListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mLL.getVisibility() == View.VISIBLE) {
                    mLL.setVisibility(View.GONE);
                } else {
                    mLL.setVisibility(View.VISIBLE);
                }
            }
        });

        initAdater();

    }

    private List<SalesBean.ListBean> mDatas = new ArrayList<>();

    private void initAdater() {
        mCommonAdapter = new CommonAdapter<SalesBean.ListBean>(this, R.layout.activity_sales_item, mDatas) {
            @Override
            protected void convert(ViewHolder holder, SalesBean.ListBean listBean, int position) {
                TextView tv_name = holder.getView(R.id.tv_name);
                TextView tv_weight = holder.getView(R.id.tv_weight);
                TextView tv_price = holder.getView(R.id.tv_price);

                tv_name.setText(listBean.getName());
                tv_weight.setText(listBean.getNum() + listBean.getUnit());
                tv_price.setText("￥" + listBean.getTotal_fee());

            }
        };
        mRecyclerView.setAdapter(mCommonAdapter);
    }

    private void beforeQuery() {
        String sdate = mQueryStart.getText().toString().trim();
        String edate = mQueryStop.getText().toString().trim();

        getDataForService(sdate, edate);
    }

    @OnClick({R.id.tv_query_start, R.id.tv_query_stop, R.id.btn_query})
    public void click(View v) {
        switch (v.getId()) {
            case R.id.tv_query_start:
                pvTime.show(mQueryStart);
                break;
            case R.id.tv_query_stop:
                pvTime2.show(mQueryStop);
                break;
            case R.id.btn_query:
                beforeQuery();
                break;
            default:
                break;
        }
    }


}
